Skip to content

python: T8859: validate min_keysize once in verify_diffie_hellman_length#5194

Draft
andamasov wants to merge 1 commit into
rollingfrom
fix/T8859-dh-keysize-validation
Draft

python: T8859: validate min_keysize once in verify_diffie_hellman_length#5194
andamasov wants to merge 1 commit into
rollingfrom
fix/T8859-dh-keysize-validation

Conversation

@andamasov

Copy link
Copy Markdown
Member

Tracked in Phorge T8859.

Defect

verify_diffie_hellman_length(file, min_keysize) in python/vyos/configverify.py wraps str(min_keysize) in try/except, but:

  1. The later int(min_keysize) call on the comparison line can still raise ValueError for non-numeric inputs (e.g. 'abc'), surfacing as an unhandled exception instead of returning False as the function intends.
  2. The local keysize variable was computed but never used — the function only consumes min_keysize afterward.

Fix

Convert min_keysize once inside try/except (TypeError, ValueError), store as min_keysize_int, and reuse for the bits comparison. Unused keysize variable dropped.

Behavior change

None for valid integer / integer-string inputs (the common case from callers in src/conf_mode/vpn_ipsec.py and similar). The only observable change is that a non-numeric min_keysize now correctly returns False instead of propagating ValueError.

Provenance

Found by Copilot during review of vyos/vyos-1x#5074 — the configverify changes were dropped from that PR per dmbaturin's scope narrowing. This defect is independent of the broader configverify redesign concerns (MTU helpers taking defaults as args) and can be fixed in isolation.

Test plan

  • CLI smoketests pass (vpn_ipsec, vpn_openconnect and any others calling verify_diffie_hellman_length).
  • Manual: pass a non-numeric min_keysize and verify the function returns False instead of raising.
  • Manual: pass a valid integer min_keysize against a known-good DH file and verify return is True when bits ≥ keysize.

🤖 Generated by robots

The previous implementation wrapped str(min_keysize) in try/except but
never used the result, and the later int(min_keysize) call on line 444
could still raise ValueError for non-numeric inputs (e.g. 'abc'),
surfacing as an unhandled exception instead of returning False.

Convert min_keysize once inside the try/except (catching TypeError and
ValueError), and reuse that value for the comparison. The unused
keysize variable is dropped.

No behavior change for valid integer / integer-string inputs; the only
observable change is that previously-uncaught ValueError on a non-numeric
min_keysize now correctly returns False as the function's contract
implies.
@coderabbitai

coderabbitai Bot commented May 13, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 93d61983-b3f3-4bcd-b8d6-f3877282a63b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch fix/T8859-dh-keysize-validation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

Copy link
Copy Markdown

👍
No issues in PR Title / Commit Title

@github-actions

Copy link
Copy Markdown

✅ No typos found in changed files.

@github-actions

Copy link
Copy Markdown

CI integration ❌ failed!

Details

CI logs

  • CLI Smoketests 👍 passed
  • CLI Smoketests (interfaces only) ❌ failed
  • Config tests 👍 passed
  • RAID1 tests 👍 passed
  • CLI Smoketests VPP ❌ failed
  • Config tests VPP 👍 passed
  • TPM tests 👍 passed

@dmbaturin dmbaturin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree validation could be better here but I don't think this solution is the best one.

One note: this function is not actually used in any real scripts. Its only callers are unit tests for this very function (https://github.com/vyos/vyos-1x/blob/current/src/tests/test_configverify.py#L25-L31). Which means we can change it freely however we want, since we only need to update two tests for that.

If we are to change it, I believe it should be like this:

  • Its signature should be def verify_diffie_hellman_length(file: str, min_keysize: int):—calling it with an argument that makes no sense is a logic problem in a script.
  • It should throw a TypeError if min_keysize is not an integer at all.
  • It should also throw a ValueError if min_keysize is not a positive integer (because key size of -512 makes no more sense than fgsfds).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

2 participants